package com.tcm.content.service.impl;

import cn.hutool.core.date.DateUtil;
import com.base.core.dozer.DozerConvert;
import com.base.core.exception.ServiceException;
import com.base.core.pager.PageBean;
import com.base.core.pager.PageContext;
import com.base.core.utils.R;
import com.base.genid.generator.UidGenerator;
import com.base.redis.JedisClient;
import com.tcm.content.dao.mapper.AppraisesMapper;
import com.tcm.content.dao.mapper.PanelMapper;
import com.tcm.content.dto.AppraisesDTO;
import com.tcm.content.dto.PanelDTO;
import com.tcm.content.entity.Appraises;
import com.tcm.content.entity.Panel;
import com.tcm.content.enums.BusinessCodeEnum;
import com.tcm.content.service.AppraisesService;
import com.tcm.content.service.PanelService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

@DubboService(version = "0.0.1-alpha", delay = -1, retries = 3)
public class AppraisesServiceImpl implements AppraisesService {

    private final static Logger log = LoggerFactory.getLogger(AppraisesServiceImpl.class);

    @Autowired
    private AppraisesMapper appraisesMapper;

    @DubboReference(version = "0.0.1-beta", group = "cached-uid", timeout = 6000, check = false, retries = 3)
    private UidGenerator uidGenerator;

    @Override
    public AppraisesDTO findById(Long id) {
        Appraises appraises = appraisesMapper.selectByPrimaryKey(id);
        if (appraises == null) {
            R.throwFail(BusinessCodeEnum.QUERY_APPRAISES_FAIL);
        }
        AppraisesDTO appraisesDTO = new AppraisesDTO();
        BeanUtils.copyProperties(appraises, appraisesDTO);
        return appraisesDTO;
    }

    @Override
    public List<AppraisesDTO> listAppraises(Long userId, Integer length) {
        List<Appraises> appraisesDTOList = appraisesMapper.listAppraises(userId, length);
        return DozerConvert.copyList(appraisesDTOList, AppraisesDTO.class);
    }

    /**
     * 获得评价列表
     *
     * @param userId
     * @param start
     * @param pagesize
     * @return
     */
    @Override
    public PageBean<AppraisesDTO> listPageAppraises(Long userId, Long start, Long pagesize) {
        Long count = appraisesMapper.countAppraises(userId);
        PageContext.setCurrentPage(start);
        PageContext.setPageSize(pagesize);
        PageContext.setDataCount(count);
        if(count.intValue() == 0){
            return new PageBean<>(null);
        }
        List<Appraises> appraisesDTOList = appraisesMapper.listPageAppraises(userId, PageContext.getStart(), pagesize);
        List<AppraisesDTO> appraisesDTOs = appraisesDTOList.stream().map(
                appraises -> {
                    AppraisesDTO appraisesDTO = new AppraisesDTO();
                    BeanUtils.copyProperties(appraises, appraisesDTO);
                    return appraisesDTO;
                }
        ).collect(Collectors.toList());
        PageContext.clear();
        return new PageBean<>(appraisesDTOs);
    }

    @Override
    public Long countAppraises(Long userId) {
        return appraisesMapper.countAppraises(userId);
    }

    @Override
    public int save(AppraisesDTO appraisesDTO) throws ServiceException  {

        Appraises appraises = new Appraises();
        BeanUtils.copyProperties(appraisesDTO, appraises);
        appraises.setCreateTime(DateUtil.date());
        appraises.setUpdateTime(DateUtil.date());
        appraises.setId(uidGenerator.getUID());
        appraises.setDeleted(new Byte("0"));
        if (appraisesMapper.insert(appraises) != 1) {
            R.throwFail(BusinessCodeEnum.ADD_APPRAISES_FAIL);
        }
        //同步缓存
        return 1;
    }

    @Override
    public int deleteLogic(Long id) {
        if (appraisesMapper.deleteLogicByPrimaryKey(id) != 1) {
            R.throwFail("删除失败");
        }
        //同步缓存
        return 1;
    }

}
